﻿using System;
using System.Data.SqlClient;
using Devv.Core.UniDataTier;
using Yglu.BusinessLayer.Entities;

namespace Yglu.BusinessLayer
{
	public partial class DataAccess
	{
		#region Get

		public static SqlDataReader Category_Get()
		{
			DataHandler db = new DataHandler();

			return (SqlDataReader)db.ExecuteReader("upCategory_Get");
		}

		public static SqlDataReader Category_Get(Int32 id)
		{
			DataHandler db = new DataHandler();

			db.AddParameter("@CategoryId", id);

			return (SqlDataReader)db.ExecuteReader("upCategory_Get");
		}

		public static SqlDataReader Category_Get(Store store)
		{
			DataHandler db = new DataHandler();

			db.AddParameter("@StoreId", store.Id);

			return (SqlDataReader)db.ExecuteReader("upCategory_Get");
		}

		#endregion

		#region Insert and update

		public static void Category_Upsert(Category category)
		{
			using (DataHandler db = new DataHandler())
			{
				db.AddParameter("@Title", category.Title);
				db.AddParameter("@Priority", category.Priority);
				db.AddParameter("@MultiSelect", category.MultiSelect);
				db.AddParameter("@AllStores", category.AllStores);
				db.AddParameter("@Active", category.Active);

				if (category.StoreId > 0)
				{
					db.AddParameter("@StoreId", category.StoreId);
				}

				if (category.Id > 0)
				{
					db.AddParameter("@CategoryId", category.Id);
					db.ExecuteScalar("upCategory_Update");
				}
				else
				{
					category.Id = Convert.ToInt32(db.ExecuteScalar("upCategory_Insert"));
				}
			}
		}

		#endregion
	}
}